﻿using System.Collections;

using UnityEngine;

public class SoundManager : MonoBehaviour {

    public AudioSource efxSource; //音效用AudioSource
    public AudioSource musicSource; //BGM用AudioSource
    public static SoundManager instance = null;
    //音量高低范围用变量
    public float lowPitchRange = .95f;
    public float highPitchRange = 1.05f;

    //单例的处理
    void Awake ( ) {
        if (instance == null)
            instance = this;
        else if (instance != this)
            Destroy (gameObject);

        DontDestroyOnLoad (gameObject);
    }

    //BGM的播放
    public void PlaySingle (AudioClip clip) {
        efxSource.clip = clip;
        efxSource.Play ( );
    }

    //获取复数的音效、随机播放
    public void RandomizeSfx (params AudioClip [ ] clips) {
        //随机获取的音效编号
        int randomIndex = Random.Range (0, clips.Length);
        //随机生成音量范围
        float randomPitch = Random.Range (lowPitchRange, highPitchRange);
        efxSource.pitch = randomPitch;
        //选择播放的音效
        efxSource.clip = clips [randomIndex];
        //播放音效
        efxSource.Play ( );
    }
}
